package com.netdisk.demo.config;


import com.netdisk.demo.security.MyPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.web.filter.CharacterEncodingFilter;

/**
 * @Author Wang
 * @Description Spring Security配置类
 * @Date 22:23 2020/5/10
 **/
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService myCustomUserService;

    @Autowired
    private MyPasswordEncoder myPasswordEncoder;

//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication()
//                .withUser("admin")
//                .password("123456")
//                .roles("admin");
//    }//测试用，账户信息储存在内存
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(this.daoAuthenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //任何界面都需授权访问
                .antMatchers("/**").authenticated()
                .antMatchers("/emps").hasRole("admin")//emps页面需要管理员权限
                //返回的用户要用ROLE_admin
                .and()
                //登录界面
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/dologin")
                //成功后重定向，不成功跳转(此处有坑，defaultsuccessurl设置的是默认跳转，
                // 在登陆之前如果你访问是上面的login界面，登陆后会返回这里的defaultSuccessUrl
                // 但如果你访问的不是登录界面，登陆后就会跳转回原来想去的界面，配置不好容易出302，
                // 如果配置了第二个参数为true就强制跳转到/index不管原先想去哪
                .defaultSuccessUrl("/index",true).failureUrl("/login?error=true")
                .permitAll()
                .and()
                .csrf().disable();
        //session管理
        http.sessionManagement().invalidSessionUrl("/login");
        http.logout().deleteCookies("JESSIONID");
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8"); filter.setForceEncoding(true);
        //
        http.addFilterBefore(filter, CsrfFilter.class);
    }


    @Override
    public void configure(WebSecurity web) throws Exception {
        //服务器资源放行
        web.ignoring().antMatchers("/js/**","/css/**","/img/**","/public/**");
    }



    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        DaoAuthenticationProvider bean = new DaoAuthenticationProvider();
        //覆盖UserDetailsService类
        bean.setUserDetailsService(myCustomUserService);
        //覆盖默认的密码验证类
        bean.setPasswordEncoder(myPasswordEncoder);
        return bean;
    }

}

